﻿// description  : 业务核心领域模型
// author       : tristan
// created      : 2011/2/17
// contact      : snaprack@gmail.com


namespace RBAC
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Linq.Expressions;

    /// <summary>
    /// 角色
    /// </summary>
    public partial class Role
    {
        /// <summary>
        /// 获得实体容器
        /// </summary>
        public EntityContext GetEntityContext()
        {
            return EntityContext.GetInstance();
        }

        /// <summary>
        /// 分配资源
        /// </summary>
        /// <param name="resource">资源</param>
        public void Assign(string resource)
        {
            Resource r = GetEntityContext().Resources.FirstOrDefault(it => it.ID == resource);
            if (r != null)
            {
                Privilege privilege = new Privilege { Role = this, Resource = r };
                GetEntityContext().AddToPrivileges(privilege);
            } 
        }

        /// <summary>
        /// 解除资源分配
        /// </summary>
        /// <param name="resource">资源</param>
        public void Unassign(string resource)
        {
            Privilege privilege = GetEntityContext().Privileges.FirstOrDefault(it => it.Role.ID == this.ID && it.Resource.ID == resource);
            if (privilege != null)
            {
                GetEntityContext().DeleteObject(privilege);
            }
        }
        /// <summary>
        /// 获得该角色可访问的资源清单
        /// </summary>
        public IEnumerable<Resource> GetResources()
        {
            return GetEntityContext().GetResourcesByRole(this.ID);
        }
        /// <summary>
        /// 载入角色实例
        /// </summary>
        /// <param name="id">角色名，不区分大小写</param>
        public static Role Load(string id)
        {
            return Find(role => role.ID == id).FirstOrDefault();
        }
        /// <summary>
        /// 载入所有的角色实例
        /// </summary>
        public static IQueryable<Role> LoadAll()
        {
            Role role = new Role();
            return role.GetEntityContext().Roles;
        }
        /// <summary>
        /// 查询角色实例
        /// </summary>
        /// <param name="predicate">查询条件</param>
        public static IQueryable<Role> Find(Expression<Func<Role, bool>> predicate)
        {
            Role role = new Role();
            return role.GetEntityContext().Roles.Where(predicate);
        }
        /// <summary>
        /// 添加角色
        /// </summary>
        public void Add()
        {
            GetEntityContext().AddToRoles(this);
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        public void Remove()
        {
            GetEntityContext().DeleteObject(this);
        }
        /// <summary>
        /// 保存更改
        /// </summary>
        public void Save()
        {
            try
            {
                GetEntityContext().SaveChanges();
            }
            catch (Exception e)
            {
                Error(e);
            }
        }
        /// <summary>
        /// 处理异常
        /// </summary>
        protected virtual void Error(Exception e)
        {
            Exception baseException = e.GetBaseException();
            if (baseException is SqlException
                && baseException.Message.Contains("违反了 PRIMARY KEY 约束"))
            {
                throw new AccessControlException("添加失败, 角色已经存在!", baseException);
            }
        }

    }


}
